﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace Swoosh
{
    public class SwooshAppSettings
    {
        public static LoggingSettings LoggingSettings
        {
            get
            {
                try
                {
                    return (LoggingSettings)ConfigurationManager.GetSection("LoggingSettings");
                }
                catch (Exception e)
                {
                    string msg = "Unable to load Logging Settings!";
                    throw new LoadSwooshSettingsException(msg, e);
                }
            }
        }
        public static ApplicationSettings ApplicationSettings
        {
            get
            {
                try
                {
                    return (ApplicationSettings)ConfigurationManager.GetSection("ApplicationSettings");
                }
                catch (Exception e)
                {
                    string msg = "Unable to load Application Settings!";
                    throw new LoadSwooshSettingsException(msg, e);
                }
            }
        }

    }

    public class LoadSwooshSettingsException : System.Exception
    {
        private const string baseMsg = "Unable to load Swoosh settings from configuration files! ";

        public LoadSwooshSettingsException() : base(baseMsg) { }
        public LoadSwooshSettingsException(string message) : base(baseMsg + message) { }
        public LoadSwooshSettingsException(string message, System.Exception inner) : base(baseMsg + message, inner) { }
        public LoadSwooshSettingsException(System.Exception inner) : base(baseMsg, inner) { }
    }

    public class LoggingSettings : ConfigurationSection
    {
        [ConfigurationProperty("ConsoleLogLevel", DefaultValue = "Info")]
        public string ConsoleLogLevel
        {
            get { return (string)this["ConsoleLogLevel"]; }
        }

        [ConfigurationProperty("FileLogLevel", DefaultValue = "Info")]
        public string FileLogLevel
        {
            get { return (string)this["FileLogLevel"]; }
        }

        [ConfigurationProperty("EventLogLevel", DefaultValue = "Warn")]
        public string EventLogLevel
        {
            get { return (string)this["EventLogLevel"]; }
        }

        [ConfigurationProperty("FileLogPath", DefaultValue = "%APPDATA%\\Swoosh\\Log")]
        public string FileLogPath
        {
            get
            {
                return SwooshSettingHelper.ReplaceFolderPlaceholders((string)this["FileLogPath"]);
            }
        }

        [ConfigurationProperty("FileLogName",  DefaultValue = "swoosh")]
        public string FileLogName
        {
            get { return (string)this["FileLogName"]; }
        }
    }

    public class ApplicationSettings : ConfigurationSection
    {

        [ConfigurationProperty("ConfigurationFile", DefaultValue = "%APPDATA%\\Swoosh\\SwooshConfiguration.xml")]
        public string ConfigurationFile
        {
            get
            {
                return SwooshSettingHelper.ReplaceFolderPlaceholders((string)this["ConfigurationFile"]);
            }
        }

        [ConfigurationProperty("ModulePath", DefaultValue = "%APPLICATIONPATH%\\Modules")]
        public string ModulePath
        {
            get
            {
                return SwooshSettingHelper.ReplaceFolderPlaceholders((string)this["ModulePath"]);
            }
        }
    }


    static class SwooshSettingHelper
    {
        public static string ReplaceFolderPlaceholders(string Folder)
        {
            string ret = Folder.Replace("%APPDATA%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            ret = ret.Replace("%APPLICATIONPATH%", Path.GetDirectoryName(Application.ExecutablePath));
            return ret;
        }

    }
}
